package com.aspose.cells;

import com.sun.media.imageio.plugins.tiff.BaselineTIFFTagSet;
import com.sun.media.imageio.plugins.tiff.TIFFDirectory;
import com.sun.media.imageio.plugins.tiff.TIFFField;
import com.sun.media.imageio.plugins.tiff.TIFFImageWriteParam;
import com.sun.media.imageio.plugins.tiff.TIFFTag;
import java.awt.image.RenderedImage;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageTypeSpecifier;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.metadata.IIOInvalidTreeException;
import javax.imageio.metadata.IIOMetadata;
import javax.imageio.stream.MemoryCacheImageOutputStream;

class zmq {
   private static final boolean a;

   public void a(com.aspose.cells.b.a.d.zm var1, ImageOrPrintOptions var2, ArrayList var3, zbuz var4) throws Exception {
      ImageWriter var5 = (ImageWriter)ImageIO.getImageWritersByFormatName("tiff").next();
      MemoryCacheImageOutputStream var6 = new MemoryCacheImageOutputStream(var1.j());
      var5.setOutput(var6);
      var5.prepareWriteSequence((IIOMetadata)null);
      TIFFImageWriteParam var7 = null;
      if (a) {
         try {
            var7 = zain.a();
            var7.setCompressionMode(2);
            switch(var2.getTiffCompression()) {
            case 1:
               var2.e = true;
               var7.setCompressionType("CCITT RLE");
               break;
            case 2:
               var7.setCompressionType("LZW");
               break;
            case 3:
               var2.e = true;
               var7.setCompressionType("CCITT T.4");
               break;
            case 4:
               var2.e = true;
               var7.setCompressionType("CCITT T.6");
            }
         } catch (Exception var16) {
            com.aspose.cells.a.e.zf.a((Throwable)var16);
         }
      }

      int var8 = -1;

      for(int var9 = 0; var9 < var3.size(); ++var9) {
         zan var10 = (zan)var3.get(var9);
         if (var10.b.m() != var8 && var8 != -1) {
            var4.a.b();
         }

         if (var9 % 50 == 0 && var9 != 0) {
            com.aspose.cells.b.a.zg.a();
            com.aspose.cells.b.a.zg.b();
         }

         try {
            com.aspose.cells.b.a.b.za var11 = var4.a(var9, var2);
            ImageTypeSpecifier var12 = ImageTypeSpecifier.createFromRenderedImage(var11.f());
            IIOMetadata var13 = var5.getDefaultImageMetadata(var12, (ImageWriteParam)null);
            var13 = this.a(var13, var11.j(), var11.k());
            if (var2.getTiffPhotometricInterpretation() > -1) {
               var13 = this.a(var13, var2.getTiffPhotometricInterpretation());
            }

            if (var2.e) {
               RenderedImage var14 = zafm.a(var11);
               var5.writeToSequence(new IIOImage(var14, (List)null, var13), var7);
            } else {
               var5.writeToSequence(new IIOImage(var11.f(), (List)null, var13), var7);
            }
         } catch (Exception var15) {
            var15.printStackTrace();
         }

         var8 = var10.b.m();
      }

      var6.flush();
      var6.close();
   }

   public void a(com.aspose.cells.b.a.d.zm var1, ImageOrPrintOptions var2, int var3, zbuz var4) throws Exception {
      com.aspose.cells.b.a.b.za var5 = null;

      try {
         var5 = var4.a(var3, var2);
         this.a(var1, var2, var5);
      } finally {
         if (var5 != null) {
            var5.d();
         }

      }

   }

   public void a(com.aspose.cells.b.a.d.zm var1, ImageOrPrintOptions var2, com.aspose.cells.b.a.b.za var3) throws Exception {
      ImageWriter var4 = (ImageWriter)ImageIO.getImageWritersByFormatName("tiff").next();
      MemoryCacheImageOutputStream var5 = new MemoryCacheImageOutputStream(var1.j());
      var4.setOutput(var5);
      var4.prepareWriteSequence((IIOMetadata)null);
      TIFFImageWriteParam var6 = null;
      if (a) {
         try {
            var6 = zain.a();
            var6.setCompressionMode(2);
            switch(var2.getTiffCompression()) {
            case 1:
               var2.e = true;
               var6.setCompressionType("CCITT RLE");
               break;
            case 2:
               var6.setCompressionType("LZW");
               break;
            case 3:
               var2.e = true;
               var6.setCompressionType("CCITT T.4");
               break;
            case 4:
               var2.e = true;
               var6.setCompressionType("CCITT T.6");
            }
         } catch (Exception var11) {
            com.aspose.cells.a.e.zf.a((Throwable)var11);
         }
      }

      try {
         ImageTypeSpecifier var7 = ImageTypeSpecifier.createFromRenderedImage(var3.f());
         IIOMetadata var8 = var4.getDefaultImageMetadata(var7, (ImageWriteParam)null);
         var8 = this.a(var8, var3.j(), var3.k());
         if (var2.getTiffPhotometricInterpretation() > -1) {
            var8 = this.a(var8, var2.getTiffPhotometricInterpretation());
         }

         if (var2.e) {
            RenderedImage var9 = zafm.a(var3);
            var4.writeToSequence(new IIOImage(var9, (List)null, var8), var6);
         } else {
            var4.writeToSequence(new IIOImage(var3.f(), (List)null, var8), var6);
         }
      } catch (Exception var10) {
         com.aspose.cells.a.e.zf.a((Throwable)var10);
      }

      var5.flush();
      var5.close();
   }

   private IIOMetadata a(IIOMetadata var1, float var2, float var3) throws IIOInvalidTreeException {
      TIFFDirectory var4 = TIFFDirectory.createFromMetadata(var1);
      BaselineTIFFTagSet var5 = BaselineTIFFTagSet.getInstance();
      TIFFTag var6 = var5.getTag(282);
      TIFFTag var7 = var5.getTag(283);
      TIFFField var8 = new TIFFField(var6, 5, 1, new long[][]{{(long)var2, 1L}});
      TIFFField var9 = new TIFFField(var7, 5, 1, new long[][]{{(long)var3, 1L}});
      var4.addTIFFField(var8);
      var4.addTIFFField(var9);
      return var4.getAsMetadata();
   }

   private IIOMetadata a(IIOMetadata var1, int var2) throws IIOInvalidTreeException {
      TIFFDirectory var3 = TIFFDirectory.createFromMetadata(var1);
      BaselineTIFFTagSet var4 = BaselineTIFFTagSet.getInstance();
      TIFFTag var5 = var4.getTag(262);
      TIFFField var6 = new TIFFField(var5, 5, 1, new long[][]{{(long)var2, 1L}});
      var3.addTIFFField(var6);
      return var3.getAsMetadata();
   }

   static {
      boolean var0 = false;

      try {
         Class.forName("com.sun.media.imageio.plugins.tiff.TIFFImageWriteParam");
         var0 = true;
      } catch (Exception var2) {
         var0 = false;
         com.aspose.cells.a.e.zf.a((Throwable)var2);
      }

      a = var0;
   }
}
